<?php namespace Home\Controller;

//个人简历控制器
class ResumeController extends CommonController{
	private $model;
	//自动执行函数
	public function __auto(){
		//实例化用户表
		$this->model=new \Common\Model\User;
		//审核权限
		$uid=$_SESSION['homeInfo']['uid'];
		$type=$this->model->where("uid={$uid}")->pluck('type');
		if($type==1) View::error('抱歉，您没有该权限',__ROOT__.'/index.html');
	}

	//个人简历主页
	public function index(){
		$uid=$_SESSION['homeInfo']['uid'];
		$data=$this->model->where("uid={$uid}")->find();
		if($data['type']==1) View::error('抱歉，您没有该权限',__ROOT__.'/index.html');
		View::with('data',$data)->make();
	}

	//简历姓名更新
	public function editName(){
		$truename=Q('post.truename');
		if ($truename) {
			$uid=$_SESSION['homeInfo']['uid'];
			$this->model->where("uid={$uid}")->update(['truename'=>$truename]);
			echo 1;
		}else{
			echo 0;
		}
	}

	//自我介绍更新
	public function editIntro(){
		$intro=Q('post.introduction');
		if (!$intro || $intro == '一句话介绍自己，告诉我为什么选择你而不是别人') {
			echo 0;
		}else{
			$uid=$_SESSION['homeInfo']['uid'];
			$this->model->where("uid={$uid}")->update(['introduction'=>$intro]);
			echo 1;
		}
	}

	//其他个人信息更新
	public function editOthers(){
		$others=Q('post.');
		if ($others) {
			$uid=$_SESSION['homeInfo']['uid'];
			$this->model
				->where("uid={$uid}")
				->update([
					'sex'=>$others['sex'],
					'age'=>$others['age'],
					'edu'=>$others['edu'],
					'tel'=>$others['tel']
				]);
			echo 1;
		}else{
			echo 0;
		}
	}

	//uploadify异步上传
	public function upload(){
		$uid=$_SESSION['homeInfo']['uid'];
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js 也就是uploadify
	        $path=$file[0]['path'];
	    	$this->model->where("uid={$uid}")->update(['portrait'=>$path]);
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
	    }
	}

	//uploadify异步删除
	public function delUpload(){
		$path=Q('post.oldImg');
		if (unlink($path)) {
			$uid=$_SESSION['homeInfo']['uid'];
			$this->model->where("uid={$uid}")->update(['portrait'=>'']);
			echo 1;
		}else{
			echo 0;
		}
	}



	//uploadify异步上传简历
	public function upResume(){
		$uid=$_SESSION['homeInfo']['uid'];
	    $file = Upload::path('Upload/Resume/' . date('y/m'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js 也就是uploadify
	        $path=$file[0]['path'];
	        $oldpath=$this->model->where("uid={$uid}")->pluck('cloneresume');
	        if($oldpath) unlink($oldpath);
	    	$this->model->where("uid={$uid}")->update(['resume'=>$path,'cloneresume'=>$path]);
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
	    }
	}

	//uploadify异步删除
	public function delResume(){
		$path=Q('post.oldResume');
		if (isset($path)) {
			$uid=$_SESSION['homeInfo']['uid'];
			$this->model->where("uid={$uid}")->update(['resume'=>'']);
			echo 1;
		}else{
			echo 0;
		}
	}


}